//
// Created by christianbraunschweiler on 21.04.13.
//
// To change the template use AppCode | Preferences | File Templates.
//


#import "SpotifySearchHandler.h"
#import "SPSearch.h"
#import "SPSession.h"
#import "SPAsyncLoading.h"
#import "SpotifySearchHandlerDelegate.h"

@interface SpotifySearchHandler()

@property (nonatomic, strong) SPSearch *spotifySearch;

@end

@implementation SpotifySearchHandler

-(id)init{
    self = [super init];
    if(self){
        //Do custom initialization here...
    }

    return self;
}

-(void)performSearch:(NSString *)searchString{
    if(!self.spotifySearch){
        self.spotifySearch = [SPSearch searchWithSearchQuery:searchString inSession:[SPSession sharedSession]];

        [SPAsyncLoading waitUntilLoaded:self.spotifySearch timeout:20.0 then:^(NSArray *loadedItems, NSArray *notLoadedItems){
            NSLog(@"Search complete");
            [self.delegate searchCompletedWithResult:loadedItems];
            self.spotifySearch = nil;
        }];
    }
}

@end